package com.example.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.example.application.BaseActivity;
import com.example.db.DBService;
import com.example.zhaccount.R;

public class RegisterUserActivity extends BaseActivity {
	private EditText edUser,edPass,edPass_more;
	private Button btnRegist;
	private OnClickListener myOnClick;
	
	private DBService mdbService;
	private long e;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_register_user);
		findView();
	}

	private void findView(){
		edUser = (EditText) findViewById(R.id.regist_user);
		edPass = (EditText) findViewById(R.id.regist_pass);
		edPass_more = (EditText) findViewById(R.id.pass_more);
		btnRegist = (Button) findViewById(R.id.regist_btn);
		
		myOnClick = new myOnClick();
		btnRegist.setOnClickListener(myOnClick);
		
		mdbService = new DBService(this);
	}
	
	class myOnClick implements OnClickListener{

		@Override
		public void onClick(View v) {
			String name = edUser.getText().toString().trim();
			String pass = edPass.getText().toString().trim();
			String pass_more = edPass_more.getText().toString().trim();
			if (name != null && !name.equals("") 
					&& pass != null && !pass.equals("")
					&& pass_more != null && !pass_more.equals("")) {
				if (pass.equals(pass_more)) {
					e = mdbService.insertUser(name, pass);
					if (e != -1) {
						Toast.makeText(RegisterUserActivity.this,
								"注册用户成功", Toast.LENGTH_SHORT).show();
						finish();
						Intent it = new Intent(RegisterUserActivity.this,
								UserNameActivity.class);
						it.putExtra("name",name);
						startActivity(it);
					}else{
						Toast.makeText(RegisterUserActivity.this,
								"注册失败!", Toast.LENGTH_SHORT).show();
					}
				}else{
					Toast.makeText(RegisterUserActivity.this,
							"两次密码输入不一样", Toast.LENGTH_SHORT).show();
				}
			}else{
				Toast.makeText(RegisterUserActivity.this,
						"用户名跟密码不能为空", Toast.LENGTH_SHORT).show();
			}
			
		}
		 
	 }
}
